package com.corner.web.controller.gzh;


import com.corner.system.service.WxgzhService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@RestController
@RequestMapping("/gzh")
@Slf4j
public class WxgzhController {
    
    @Autowired
    private WxgzhService wxService;

    /**
     * get验证微信服务器来源
     * @param signature
     * @param timestamp
     * @param nonce
     * @param echostr
     * @return
     */
    @GetMapping(produces = "text/plain;charset=utf-8")
    public String authGet(
                          @RequestParam(name = "signature", required = false) String signature,
                          @RequestParam(name = "timestamp", required = false) String timestamp,
                          @RequestParam(name = "nonce", required = false) String nonce,
                          @RequestParam(name = "echostr", required = false) String echostr) {

        log.info("\n接收到来自微信服务器的认证消息：[{}, {}, {}, {}]", signature,
                timestamp, nonce, echostr);
        //如果是post请求 echostr为空
        Boolean result = false;
        result = wxService.wxSignatureCheck(signature, timestamp, nonce, echostr);
        if(result){
            return echostr;
        }
        return "";
    }

    /**
     *
     * @param request
     * @param response
     * @param signature
     * @param timestamp
     * @param nonce
     * @param echostr
     * @return
     */
    @PostMapping(produces = "text/plain;charset=utf-8")
    public String getMessage(HttpServletRequest request, HttpServletResponse response,@RequestParam(name = "signature", required = false) String signature,
                             @RequestParam(name = "timestamp", required = false) String timestamp,
                             @RequestParam(name = "nonce", required = false) String nonce,
                             @RequestParam(name = "echostr", required = false) String echostr) throws Exception{
        log.info("\n接收到来自微信服务器的认证消息：[{}, {}, {}, {}]", signature,
                timestamp, nonce, echostr);
        Boolean result = false;
        result = wxService.wxSignatureCheck(signature, timestamp, nonce, echostr);

        String messageToUser = null;
        if(result){
            messageToUser = wxService.getUserMessage(request,response);
        }

        System.out.println(messageToUser);
        return messageToUser;
    }



    /**
     * 获取微信签名
     * @param timestamp
     * @param noncestr
     * @param url
     * @return
     */
    @GetMapping("/getSignature")
    public String getSignature(
            @RequestParam(name = "timestamp", required = false) String timestamp,
            @RequestParam(name = "noncestr", required = false) String noncestr,
            @RequestParam(name = "url", required = false) String url) {

        String signature = wxService.getSignature(timestamp, noncestr, url);
        System.out.println("获取签名====》"+signature);
        return signature;
    }






}






